{% extends "projects/base_project.html" %}

{% load core_tags i18n %}

{% block title %}{% blocktrans with query=query|default:"" %}Search: {{ query }} {% endblocktrans %}{% endblock %}

{% block extra_links %}
{{ super }}

<style>
.navigable > ul > li { border-bottom: 0; }
</style>

{% endblock %}

{% block content %}

<div class="navigable">
<ul>
    <li class="{% if type == 'project' %}active{% endif %}"><a href="?{% url_replace request 'type' 'project' %}">{% trans 'Projects' %}</a></li>
    <li class="{% if type == 'file' %}active{% endif %}"><a href="?{% url_replace request 'type' 'file' %}">{% trans 'Files' %}</a></li>

    <hr>

    {% if type == 'project' %}

      {% if facets.language %}
        <h5>{% trans 'Language' %}</h5>
          {% for name, count in facets.language.items %}
            <li class="{% if language == name %}active{% endif %} language-list">
              {% if language == name %}
                <a href="?{% url_replace request 'language' ''  %}">{{ name }}
              {% else %}
                <a href="?{% url_replace request 'language' name  %}">{{ name }}
              {% endif %}
              ({{ count }})
              </a>
           </li>
          {% endfor %}
      {% endif %}

    {% endif %}

  {% if type == 'file' %}

    {% if facets.project %}
      <h5>{% trans 'Projects' %}</h5>
        {% for name, count in facets.project.items %}
          <li class="{% if project == name %}active{% endif %} project-list">
            {% if project == name %}
              <a href="?{% url_replace request 'project' ''  %}">{{ name }}
            {% else %}
              <a href="?{% url_replace request 'project' name  %}">{{ name }}
            {% endif %}
            ({{ count }})
            </a>
         </li>
        {% endfor %}

    {% endif %}

    {% if facets.version %}

      <h5>{% trans 'Version' %}</h5>
        {% for name, count in facets.version.items %}
          <li class="{% if version == name %}active{% endif %} version-list">
            {% if version == name %}
              <a href="?{% url_replace request 'version' ''  %}">{{ name }}
            {% else %}
              <a href="?{% url_replace request 'version' name  %}">{{ name }}
            {% endif %}            
            ({{ count }})
            </a>
         </li>
        {% endfor %}


    {% endif %}

    {% if facets.taxonomy %}

      <h5>{% trans 'Taxonomy' %}</h5>
        {% for name, count in facets.taxonomy.items %}
          <li class="{% if taxonomy == name %}active{% endif %}">
            {% if taxonomy == name %}
              <a href="?{% url_replace request 'taxonomy' ''  %}">{{ name }}
            {% else %}
              <a href="?{% url_replace request 'taxonomy' name  %}">{{ name }}
            {% endif %}        
            ({{ count }})
            </a>
         </li>
        {% endfor %}


    {% endif %}

  {% endif %}
  
{% block sponsor %}
<div class="search-sponsored">
Search is sponsored by <a href="https://www.elastic.co/">Elastic</a>, and hosted on <a href="https://www.elastic.co/cloud">Elastic Cloud</a>.
</div>
{% endblock %}

</ul>
  <div>
        <h2>{% trans 'Search' %}</h2>
        <!-- BEGIN search form -->
        <div class="wide-search-bar">
          <div class="wide-search-bar-wrapper clearfix">
            <form action="." method="GET">
              <div class="text-input-wrapper">
                <input type="text" name="q" value="{{ query|default_if_none:"" }}" id="id_elastic_project_search">
                {% if type %} <input type="hidden" name="type" value="{{ type }}"> {% endif %}
                {% if project %} <input type="hidden" name="project" value="{{ project }}"> {% endif %}
                {% if taxonomy %} <input type="hidden" name="taxonomy" value="{{ taxonomy }}"> {% endif %}
                {% if version %} <input type="hidden" name="version" value="{{ version }}"> {% endif %}
                {% if language %} <input type="hidden" name="language" value="{{ language }}"> {% endif %}
              </div>
              <div class="submit-input-wrapper">
                {% comment %}Translators: This is about starting a search{% endcomment %}
                <input type="submit" value="{% trans 'Search' %}">
              </div>
            </form>
          </div>
        </div>
        <!-- END search form -->


        {% if query %}
        <!-- BEGIN search results -->
        <div class="module">
          <div class="module-wrapper">

            <div class="module-header">
              <h3>{% blocktrans with query=query|default:"" %}Results for {{ query }}{% endblocktrans %}</h3>
            </div>

            <div class="module-list">
              <div class="module-list-wrapper">

                <ul>
                  {% for result in results.hits.hits %}
                        <li class="module-item">
                          <p class="module-item-title">
                            {% if result.fields.name %} 

                              {# Project #}
                              <a href="{{ result.fields.url }}">{{ result.fields.name }}</a>
                              {% for fragment in result.highlight.description|slice:":3" %}
                                <p>
                                ...{{ fragment|safe }}...
                                </p>
                                <br>
                              {% endfor %}
                              {# End Project #}

                            {% elif result.fields.path %}

                              {# File #}
                              <a href="{% doc_url result.fields.project|get_project result.fields.version result.fields.path %}?highlight={{ query }}">{{ result.fields.project }} - {{ result.fields.title|safe }}</a>
                              {% for fragment in result.highlight.content|slice:":3" %}
                                <p>
                                ...{{ fragment|safe }}...
                                </p>
                              {% endfor %}
                              {# End File #}

                            {% endif %}
                          </p>
                        </li>
                  {% empty %}
                    <li class="module-item"><span class="quiet">{% trans "No results found. Bummer." %}</span></li>
                  {% endfor %}

                </ul>

              </div>
            </div>

          </div>
        </div>
        <!-- END search results -->

          {% if page.has_previous or page.has_next %}
          <!-- BEGIN search pagination -->
          <div class="pagination">
            {% if page.has_previous %}
              <a href="?q={{ query }}&amp;page={{ page.previous_page_number }}">&laquo; {% trans "Previous" %}</a>
            {% else %}
              <span class="disabled">&laquo; {% trans "Previous" %}</span>
            {% endif %}

            {% if page.has_next %}
              <a class="next" href="?q={{ query }}&amp;page={{ page.next_page_number }}">{% trans "Next" %} &raquo;</a>
            {% else %}
              <span class="next disabled">{% trans "Next" %} &raquo;</span>
            {% endif %}
          </div>
          <!-- END search pagination -->
          {% endif %}

        {% else %}
          {# Show some example queries to run, maybe query syntax, something else? #}
        {% endif %}


  </div>
</div>
{% endblock %}
